<template>
  <div class="yl-container">
    <div class="yl-main">
      <!-- 搜索模块 -->
      <div class="yl-search-list">
        <el-form :model="searchConfig" ref="searchConfig" :inline="true">
          <el-form-item>
            <el-input v-model="searchConfig.name" placeholder="请输入角色名称" type="text" size="small"></el-input>
          </el-form-item>
          <el-form-item>
            <YlButton uiType="default" icon="el-icon-search" @click="handleSearch">搜 索</YlButton>
            <YlButton uiType="default" btnType="success" icon="iconfont yl-ecs-zhongzhi yl-icon" @click="resetSearch">重 置</YlButton>
          </el-form-item>
        </el-form>
      </div>

      <!-- 按钮组 -->
      <div class="yl-btn-group">
        <YlButton uiType="plain" icon="el-icon-circle-plus-outline" @click.native="initAdd">新增</YlButton>
      </div>
      <!-- table 列表组 -->
      <div class="yl-table-list">
        <el-table
          v-loading="loading"
          element-loading-text="玩命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          border
          stripe
          style="width: 100%"
          :data="tableData"
        >
          <el-table-column align="center" label="序号" width="80px" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.$index + 1 }}</span>
            </template>
          </el-table-column>

          <el-table-column align="center"  label="角色名称" prop="nns_role_name" show-overflow-tooltip></el-table-column>

          <el-table-column align="center"  label="创建者" prop="nns_creator" show-overflow-tooltip></el-table-column>

          <el-table-column align="center"  label="绑定账号数" prop="nns_used_num" show-overflow-tooltip></el-table-column>

          <el-table-column align="center"  label="操作" width="320px" fixed="right">
            <template slot-scope="scope">
              <YlButton uiType="main" icon="iconfont yl-ecs-bianji yl-icon" @click="handleChange(scope.row)">编辑</YlButton>
              <YlButton uiType="main" btnType="success" icon="iconfont yl-ecs-chakan yl-icon" @click="handleShowAccout(scope.row.nns_id)">查看绑定的账号</YlButton>
              <YlButton uiType="main" btnType="danger" icon="iconfont yl-ecs-shanchu yl-icon" @click="handleDelete(scope.row.nns_id)">删除</YlButton>
            </template>
          </el-table-column>
          <div class="yl-table-empty" slot="empty">
            <img src="~@/assets/icons/svg/no-data.svg">
            <div>暂无数据</div>
          </div>
        </el-table>
        <!-- 分页模块 -->
        <YlPage :size="size" :page="page" :total="total" @page-size="getPageSize" @current-page="getCurrentPage"></YlPage>
      </div>
      <!-- 新增修改dialog -->
      <el-dialog :title="formConfig.isFix ? '修改角色' : '新增角色'" width="1184px" :visible.sync="dialogFormVisible" class="yl-dialog">
        <YlForm
          ref="ylform"
          :formConfig="formConfig"
          @close-dialog="getCloseDialog"
          @init-state="getInitState"
        />
      </el-dialog>

      <!-- 查看绑定的角色 -->
      <el-dialog title="已绑定的账号" :visible.sync="dialogTableVisible" class="yl-dialog">
        <el-table
          :data="accountData"
        >
          <el-table-column align="center"  property="nns_name" label="用户名" width="150"></el-table-column>
          <el-table-column align="center"  property="nns_account" label="账号" width="200"></el-table-column>
          <el-table-column align="center"  label="状态">
            <template slot-scope="scope">
              <span class="success" v-if="scope.row.nns_state == 1">正常状态</span>
              <span class="info" v-else>禁用状态</span>
            </template>
          </el-table-column>
          <div class="yl-table-empty" slot="empty">
            <img src="~@/assets/icons/svg/no-data.svg">
            <div>暂无数据</div>
          </div>
        </el-table>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      accountData: [],
      size: 5,
      page: 1,
      total: 0,
      searchConfig: {
        name: ''
      },
      tableData: [],
      // 新增 修改 弹窗
      dialogFormVisible: false,
      // 查看绑定的账号
      dialogTableVisible: false,
      formConfig: {
        isFix: false,
        changeId: '',
        btnMethod: ['submit', 'cancel'],
        formData: {
          role_name: '',
          pris_no: []
        },
        columns: [
          {
            type: 'input',
            label: '角色名称',
            prop: 'role_name'
          },
          {
            type: 'tree',
            label: '菜单权限树',
            prop: 'pris_no',
            treeConfig: {
              hasMenuBtn: false,
              hasMenuInfo: false,
              hasCheckBox: true,
              node_key: 'nns_menu_no',
              defaultCheckedKeys: [],
              treeData: []
            }
          }
        ],
        editRules: {
          role_name: [
            { required: true, message: '请输入角色名称', trigger: 'blur' }
          ],
          pris_no: [
            { required: true, message: '请选择菜单权限', trigger: 'change' }
          ]
        },
        addApi: this.$ylapi.addNewRole,
        changeApi: this.$ylapi.changeRoleInfo
      }
    }
  },
  mounted () {
    this.initTable()
  },
  methods: {
    initAdd () {
      this.formConfig.isFix = false
      this.dialogFormVisible = true
      this.formConfig.formData.role_name = ''
      this.formConfig.formData.pris_no = []
      this.formConfig.btnMethod = ['submit', 'cancel']
      this.$nextTick(() => {
        this.$refs['ylform'].$refs['yltree'][0].$refs['tree'].setCheckedKeys([])
      })
    },
    initTable () {
      this.page = 1
      this.size = 5
      this.loading = true
      this.dialogFormVisible = false
      this.searchConfig.name = ''
      this.initData()
      this.initMenu()
    },
    async initData () {
      let { page, total, list } = await this.$ylapi.getRoleList({
        'page': this.page,
        'page_size': this.size,
        ...this.searchConfig
      })
      this.page = page
      this.total = total
      this.tableData = list
      this.loading = false
    },
    // 获取菜单
    async initMenu () {
      this.formConfig.columns[1].treeConfig.treeData = await this.$ylapi.getPersonalMenu()
    },
    // 删除
    async handleDelete (id) {
      // eslint-disable-next-line no-unused-expressions
      await this.$modal.confirm(this, {
        api: this.$ylapi.deleteRole,
        obj: id
      })
        ? this.initTable()
        : ''
    },
    // 修改
    handleChange (row) {
      this.dialogFormVisible = true
      this.formConfig.isFix = true
      this.formConfig.btnMethod = ['save', 'cancel']
      this.formConfig.changeId = row.nns_id
      // 重置
      this.$nextTick(() => {
        this.$refs['ylform'].$refs['yltree'][0].$refs['tree'].setCheckedKeys([])
        this.formConfig.columns[1].treeConfig.defaultCheckedKeys = row.nns_pris_no.split(',')
      })
      this.formConfig.formData.pris_no = row.nns_pris_no.split(',')
      this.formConfig.formData.role_name = row.nns_role_name
    },
    // 查看绑定的账号
    async handleShowAccout (id) {
      this.dialogTableVisible = true
      this.accountData = await this.$ylapi.getBindUser(id)
    },
    // 搜索
    handleSearch () {
      this.loading = true
      this.initData()
    },
    // 重置搜索
    resetSearch () { this.initTable() },
    // 初始化
    getInitState (val) {
      if (val) {
        this.initTable()
      }
    },
    // 每页显示条数
    getPageSize (val) {
      this.size = val
      this.loading = true
      this.initData()
    },
    // 下一页
    getCurrentPage (val) {
      this.page = val
      this.loading = true
      this.initData()
    },
    // 关闭弹窗
    getCloseDialog (val) { this.dialogFormVisible = val }
  }
}
</script>
